﻿using System;

namespace UniRx.Operators
{
	// Token: 0x0200029A RID: 666
	internal class DoOnTerminateObservable<T> : OperatorObservableBase<T>
	{
		// Token: 0x06000EB6 RID: 3766 RVA: 0x000451C0 File Offset: 0x000435C0
		public DoOnTerminateObservable(IObservable<T> source, Action onTerminate) : base(source.IsRequiredSubscribeOnCurrentThread<T>())
		{
			this.source = source;
			this.onTerminate = onTerminate;
		}

		// Token: 0x06000EB7 RID: 3767 RVA: 0x000451DC File Offset: 0x000435DC
		protected override IDisposable SubscribeCore(IObserver<T> observer, IDisposable cancel)
		{
			return new DoOnTerminateObservable<T>.DoOnTerminate(this, observer, cancel).Run();
		}

		// Token: 0x04000889 RID: 2185
		private readonly IObservable<T> source;

		// Token: 0x0400088A RID: 2186
		private readonly Action onTerminate;

		// Token: 0x0200029B RID: 667
		private class DoOnTerminate : OperatorObserverBase<T, T>
		{
			// Token: 0x06000EB8 RID: 3768 RVA: 0x000451EB File Offset: 0x000435EB
			public DoOnTerminate(DoOnTerminateObservable<T> parent, IObserver<T> observer, IDisposable cancel) : base(observer, cancel)
			{
				this.parent = parent;
			}

			// Token: 0x06000EB9 RID: 3769 RVA: 0x000451FC File Offset: 0x000435FC
			public IDisposable Run()
			{
				return this.parent.source.Subscribe(this);
			}

			// Token: 0x06000EBA RID: 3770 RVA: 0x0004520F File Offset: 0x0004360F
			public override void OnNext(T value)
			{
				this.observer.OnNext(value);
			}

			// Token: 0x06000EBB RID: 3771 RVA: 0x00045220 File Offset: 0x00043620
			public override void OnError(Exception error)
			{
				try
				{
					this.parent.onTerminate();
				}
				catch (Exception error2)
				{
					try
					{
						this.observer.OnError(error2);
					}
					finally
					{
						base.Dispose();
					}
					return;
				}
				try
				{
					this.observer.OnError(error);
				}
				finally
				{
					base.Dispose();
				}
			}

			// Token: 0x06000EBC RID: 3772 RVA: 0x000452A4 File Offset: 0x000436A4
			public override void OnCompleted()
			{
				try
				{
					this.parent.onTerminate();
				}
				catch (Exception error)
				{
					this.observer.OnError(error);
					base.Dispose();
					return;
				}
				try
				{
					this.observer.OnCompleted();
				}
				finally
				{
					base.Dispose();
				}
			}

			// Token: 0x0400088B RID: 2187
			private readonly DoOnTerminateObservable<T> parent;
		}
	}
}
